package com.example.view.module.event.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import com.example.common.utils.L;
import com.example.view.utils.TouchEventUtil;

public class TouchEventChild extends LinearLayout {

    private Scroller scroller;
    private View childView;
    private float startX;
    private float startY;

    public TouchEventChild(Context context) {
        super(context);
    }

    public TouchEventChild(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        childView = getChildAt(0);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            childView.scrollTo(scroller.getCurrX(), scroller.getCurrY());
            invalidate();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        L.e("Child | dispatchTouchEvent --> " + TouchEventUtil.getTouchAction(ev.getAction()));
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        L.e("Child | onInterceptTouchEvent --> " + TouchEventUtil.getTouchAction(ev.getAction()));
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        L.e("Child | onTouchEvent --> " + TouchEventUtil.getTouchAction(event.getAction()));
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                L.e("========ACTION_DOWN=========" + startX + "," + startY);
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = event.getX();
                float endY = event.getY();
                float distanceX = endX - startX;
                float distanceY = endY - startY;
                L.e("getScrollX============================" + getScrollX());
                float dx = getScrollX() - distanceX;
                L.e("( " + startX + ",,," + startY + ")" + "--(" + endX + "---" + endY + ")--(" + distanceX + ",,," + distanceY + ")" + ",DX===" + dx);
//                childView.scrollTo((int) dx, (int) 0);        //移动的是相当坐标
                childView.scrollBy((int) dx, 0);            //移动的是一下断的距离
                startX = event.getX();
                startY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                int totalScrollX = childView.getScrollX();
                L.e("TouchEvent-ACTION_UP:" + childView.getScaleX());
                //int dx =  目标- getScrollX()
                //int X = -childView.getMeasuredWidth() - childView.getScrollX();
                int x = 0 - childView.getScrollX();
                L.e("( " + childView.getScrollX() + ",,," + childView.getScrollY() + ")" + "--(" + X + ",,," + 0 + ")" + ",childViewWidth=" + childView.getMeasuredWidth());
                scroller.startScroll(childView.getScrollX(), childView.getScrollY(), x, 0);
                invalidate();
                break;
        }
        return true;
    }
}
